<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list offset="50" :immediate-check="false" v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <ArtcleItem @click.native="itemClickFn(item.art_id)" @reportEV="reportFn" @disLikeEV="disLikeFn" v-for="item in articleList" :key="item.art_id" :obj="item"></ArtcleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArtcleItem from '@/components/ArtcleItem'
import { articlesListAPI, dislikeArticleAPI, reportArticleAPI } from '@/api/index.js'
import { Notify } from 'vant'

export default {
  name: 'ArtcleList',
  data () {
    return {
      articleList: [],
      loading: false, // 底部加载状态
      finished: false, // 底部是否完成
      theTime: new Date().getTime(),
      isLoading: false
    }
  },
  components: {
    ArtcleItem
  },
  props: {
    channelId: Number
  },
  async created () {
    this.getArtcleFn()
  },
  methods: {
    async getArtcleFn () {
      const { data: res } = await articlesListAPI({
        channelId: this.channelId,
        timestamp: this.theTime
      })
      this.articleList = [...this.articleList, ...res.data.results]
      this.theTime = res.data.pre_timestamp
      this.loading = false
      if (res.data.pre_timestamp === null) {
        this.finished = true
      }
      this.isLoading = false
    },
    async onLoad () {
      if (this.articleList.length === 0) {
        return // 如果页面没有数据，没有高度，让本次onLoad逻辑代码不往下执行
      }
      this.getArtcleFn()
    },
    async onRefresh () {
      // list数组清空，换新的数据
      this.articleList = []
      this.theTime = new Date().getTime()
      this.getArtcleFn()
    },
    async disLikeFn (id) {
      await dislikeArticleAPI({
        artId: id
      })
      Notify({ type: 'success', message: '反馈成功' })
    },
    // 反馈垃圾内容
    async reportFn (id, value) {
      await reportArticleAPI({
        artId: id,
        type: value
      })
      Notify({ type: 'success', message: '举报成功' })
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  }
}
</script>

<style>
</style>
